package com.drojian.operator;

import org.junit.Test;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class TransOperator {
    @Test
    public void Test1() {
        System.out.println("======================");
        TransOperator operator = new TransOperator();
        operator.Test2();
        operator.Test3();
        operator.Test4();

        System.out.println("======================");
    }

    Observer observer = new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
            System.out.println("onSubscribe:" + d);
        }

        @Override
        public void onNext(Object o) {
            System.out.println("onNext:" + o);
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("onError:" + e);
        }

        @Override
        public void onComplete() {
            System.out.println("onComplete:");
        }
    };

    public void Test2() {
        //map直接对时间进行处理，再发射
        Observable.just("aaa")
                .map(new Function<String, Object>() {
                    @Override
                    public Object apply(String s) throws Exception {
                        return "bbb";
                    }
                }).subscribe(observer);
    }
    public void Test3() {
        //flatMap请求结果再请求
        Observable.just("aaa")
                .flatMap(new Function<String, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(String s) throws Exception {
                        return Observable.just("ccc");
                    }
                }).subscribe(observer);
    }
    public void Test4() {
        //concatMap (有序)  flatMap无序
        Observable.just("111","222","333","444","555")
                .concatMap(new Function<String, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(String s) throws Exception {
                        return Observable.just(s+"ccc");
                    }
                }).subscribe(observer);
    }


}
